package gamedsl.game;

import gamedsl.event.Event;
import gamedsl.event.EventTracker;
import net.phys2d.raw.CollisionEvent;
import org.newdawn.penguin.*;
import org.newdawn.slick.SlickException;

public class MushroomReward extends RectangularEntity{
	
	public MushroomReward(float x, float y, float width, float height, float mass, String name, String type, int value) throws SlickException{
		super(x,y,width,height,mass,name,type, "res/mushroom.gif",value);
		
		
	}
	
	public void update(int delta) {
		//Check if collision
		checkCollision();
		
	}
	
	private void checkCollision(){
		CollisionEvent[] events = world.getContacts(body);
		for(int i = 0; i < events.length; i++){
			Object b = events[i].getBodyA().getUserData();
			AbstractEntity e = (AbstractEntity)b;
			if(e != null){
				//Cases
				//  Collision with Player
				if(e.getType() == "Player"){
					System.out.println("Player hit " + this.getType() + " " + this.getId());
					//trigger MushroomHitEvent
					EventTracker.getInstance().subscribe("MushroomRewardHitEvent", this);
					EventTracker.getInstance().triggerEvent("MushroomRewardHitEvent", new Event("MushroomRewardHitEvent", this, e));
				}
				
				//Other cases
				// E.g. Collision with other actors
			}
			
		}
		
		
	}
	@Override
	public void handleEvent(Event event) {
		String message = event.getMessage();
		if(message == "MushroomRewardHitEvent"){
			System.out.println("Mushroom handle event: " + message);
			//insert logic to handle the MushroomHitEvent
			Entity objSrc = (Entity)event.getObjectSrc();
			world.remove(objSrc.getBody());
			EventTracker.getInstance().unsubscribeAll("MushroomRewardHitEvent", objSrc.getId());
			return;
		}
		//other cases
		if(message == ""){
			System.out.println("Mushroom handle event: Nothing to handle");
			return;
		}
	}
	
	
	
	
}
